package com.hhkj.api.train.common.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.hhkj.admin.question.common.cont.QuestionTypeEnum;
import com.hhkj.admin.question.domain.Question;
import com.hhkj.admin.question.model.res.QuestionRes;
import com.hhkj.api.train.model.info.res.ApiQuestionRes;

import java.util.ArrayList;
import java.util.List;

/**
 * QuestionListUtils
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/5/26 8:36
 */
public class ApiQuestionCollectionConverter
{


    /**
     * 题目列表转化给前端需要得列表形式
     *
     * @param questionGroupList 题目组列表
     * @return 题目信息列表
     */
    public static List<ApiQuestionRes> convertList(List<QuestionRes> questionGroupList)
    {
        List<ApiQuestionRes> resultList = new ArrayList<>();
        ApiQuestionRes apiQuestionRes;
        for (QuestionRes questionGroup : questionGroupList) {
            if (QuestionTypeEnum.CASE.getCode().equals(questionGroup.getTypeCode())) {
                if (CollectionUtil.isNotEmpty(questionGroup.getQuestionList())) {
                    String caseMaterials = questionGroup.getContent();
                    for (Question question : questionGroup.getQuestionList()) {
                        apiQuestionRes = BeanUtil.toBean(question, ApiQuestionRes.class);
                        if (StrUtil.isNotBlank(caseMaterials)) {
                            apiQuestionRes.setCaseMaterials(caseMaterials);
                            caseMaterials = null;
                        }
                        resultList.add(apiQuestionRes);
                    }
                }
            } else {
                resultList.add(BeanUtil.toBean(questionGroup, ApiQuestionRes.class));
            }
        }
        return resultList;
    }
}
